﻿using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using WFramework.ORM;

namespace MiniWeb.Dao
{
    public class NewsTypeDao : BaseDao<NewsType>
    {
        public PagedList<NewsType> GetPagedList(int? rid, int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Select("*").From("NewsType with(nolock)").Where("1=1");
            if (rid != null)
            {
                sql.Where("ParentType = @0", rid);
            }
            sql.OrderBy("ParentType DESC");
            return PagedList<NewsType>(pageIndex, pageSize, sql);
        }

        public List<NewsType> GetNewsList()
        {
            var sql = Sql.Builder.Where("1 =1");
            return Query<NewsType>(sql).ToList();
        }

        public NewsType GetById(int id)
        {
            var sql = Sql.Builder.Where("id = @0", id);
            return FirstOrDefault(sql);
        }

        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            Delete(sql);
        }


        public bool Exists(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }

        public List<NewsType> GetAll(int? top, string strwhere,string sort)
        {
            string sqltop = "";
            if (top != null)
            {
                sqltop = "TOP " + top.ToString() + " * ";
            }
            else
            {
                sqltop = " * ";
            }
            var sql = Sql.Builder.Select(sqltop).From(" NewsType WITH(NOLOCK)");
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql.Where(strwhere);
            }
            if(!string.IsNullOrEmpty(sort))
            {
                sql.OrderBy(sort);
            }
            else
            {
                sql.OrderBy("Sort");
            }
            return Query<NewsType>(sql).ToList();
        }



    }
}